/*
 * Filename    : State_Main_Menu.java
 * Projectname : RamaShinta
 * Date        : Nov 18, 2012 , 8:51:59 PM 
 * 
 */

package com.amikomgamedev.ramashinta.state;

import org.andengine.entity.modifier.MoveYModifier;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.input.touch.TouchEvent;

import com.amikomgamedev.ramashinta.ServerData;
import com.amikomgamedev.ramashinta.Utils;

import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;

public class State_Menu_Main extends State_Menu_Select_Stage
{
	private static final int	BUTTON_PLAY		= 0;
	private static final int	BUTTON_HELP		= 1;
	private static final int	BUTTON_CREDIT	= 2;
	private static final int	BUTTON_MULTI	= 3;
	private boolean playDown = false;
	private boolean HelpDown = false;
	private boolean CreditDown = false;
	private boolean MultiDown = false;

	protected void create_Menu_Main()
	{
		// attach something for main menu
		graphic.attachMenuUtama();
		text.attachBetaVersion();
		graphic.spr_Menu_Bg.attachChild(text.Beta_Version);
		text.Beta_Version.setPosition(Utils.getRatioW(600), Utils.getRatioH(50));
		text.Beta_Version.setScale(1.5f);
		graphic.spr_Button_Menu[BUTTON_PLAY].setSkew(Utils.getRatioW(0));
		sound.playMenuBgm();
	}

	@Override
	public void onUpdate(float pSecondsElapsed)
	{
		if (State_Menu_Current == STATE_MENU_MAIN)
		{

		}

		super.onUpdate(pSecondsElapsed);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (State_Menu_Current == STATE_MENU_MAIN)
		{
			if (keyCode == KeyEvent.KEYCODE_BACK)
			{
				finish();
				Utils.TRACE("exit menu");
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX,
			float pTouchAreaLocalY)
	{
		if (State_Menu_Current == STATE_MENU_MAIN)
		{
			if (pSceneTouchEvent.isActionDown())
			{
				if (pTouchArea.equals(graphic.spr_Gear))
				{
					if(graphic.spr_Facebook.getY()==graphic.spr_Gear.getY()-graphic.spr_Facebook.getHeight()){
						graphic.spr_Gear.registerEntityModifier(new RotationModifier(1f, 0, 360));
						graphic.spr_Facebook.registerEntityModifier(new MoveYModifier(0.2f, graphic.spr_Facebook.getY(), graphic.spr_Facebook.getY()-Utils.getRatioH(30)));
						graphic.spr_Facebook.registerEntityModifier(new MoveYModifier(0.8f, graphic.spr_Facebook.getY(), graphic.spr_Gear.getY()));
					}
					if(graphic.spr_Facebook.getY()==graphic.spr_Gear.getY()){
						graphic.spr_Gear.registerEntityModifier(new RotationModifier(1f, 0, 360));
						graphic.spr_Facebook.registerEntityModifier(new MoveYModifier(1f, graphic.spr_Facebook.getY(), graphic.spr_Gear.getY()-graphic.spr_Facebook.getHeight()));
					}
					
					if(graphic.spr_Twitter.getY()==graphic.spr_Gear.getY()-(graphic.spr_Twitter.getHeight()*2)){
						graphic.spr_Twitter.registerEntityModifier(new MoveYModifier(0.2f, graphic.spr_Twitter.getY(), graphic.spr_Twitter.getY()-Utils.getRatioH(30)));
						graphic.spr_Twitter.registerEntityModifier(new MoveYModifier(0.8f, graphic.spr_Twitter.getY(), graphic.spr_Gear.getY()));
					}
					if(graphic.spr_Twitter.getY()==graphic.spr_Gear.getY()){
						graphic.spr_Twitter.registerEntityModifier(new MoveYModifier(1f, graphic.spr_Twitter.getY(), graphic.spr_Gear.getY()-(graphic.spr_Twitter.getHeight()*2)));
					}
				}

			if (pTouchArea.equals(graphic.spr_Button_Menu[BUTTON_PLAY]))
			{
				graphic.spr_Button_Menu[BUTTON_PLAY].setVisible(false);
				graphic.spr_Button_Menu_Pressed[BUTTON_PLAY].setVisible(true);
				Utils.TRACE("Button PLAY klik");
				sound.playSoundPatah();
				playDown = true;
				HelpDown = false;
				CreditDown = false;
				MultiDown = false;
			}
			
			if (!pTouchArea.equals(graphic.spr_Button_Menu[BUTTON_PLAY]))
			{
				graphic.spr_Button_Menu[BUTTON_PLAY].setVisible(true);
				graphic.spr_Button_Menu_Pressed[BUTTON_PLAY].setVisible(false);
				Utils.TRACE("Button PLAY klik");
			}
			if (pTouchArea.equals(graphic.spr_Button_Menu[BUTTON_MULTI]))
			{
				sound.playSoundRacun();
				Utils.TRACE("Button Multi");
			}
			if (pTouchArea.equals(graphic.spr_Button_Menu[BUTTON_CREDIT]))
			{
				Utils.TRACE("Button CREDIT");
				graphic.spr_Button_Menu[BUTTON_CREDIT].setVisible(false);
				graphic.spr_Button_Menu_Pressed[BUTTON_CREDIT].setVisible(true);
				Utils.TRACE("Button PLAY klik");
				sound.playSoundPatah();
				playDown = false;
				HelpDown = false;
				CreditDown = true;
				MultiDown = false;
				
			}
			if (!pTouchArea.equals(graphic.spr_Button_Menu[BUTTON_CREDIT]))
			{
				graphic.spr_Button_Menu[BUTTON_CREDIT].setVisible(true);
				graphic.spr_Button_Menu_Pressed[BUTTON_CREDIT].setVisible(false);
				Utils.TRACE("Button PLAY klik");
			}
			if (pTouchArea.equals(graphic.spr_Button_Menu[BUTTON_HELP]))
			{
				graphic.spr_Button_Menu[BUTTON_HELP].setVisible(false);
				graphic.spr_Button_Menu_Pressed[BUTTON_HELP].setVisible(true);
				sound.playSoundPatah();
				playDown = false;
				HelpDown = true;
				CreditDown = false;
				MultiDown = false;
			}
			if (!pTouchArea.equals(graphic.spr_Button_Menu[BUTTON_HELP]))
			{
				graphic.spr_Button_Menu[BUTTON_HELP].setVisible(true);
				graphic.spr_Button_Menu_Pressed[BUTTON_HELP].setVisible(false);
				Utils.TRACE("Button PLAY klik");
			}
		}
			
		
		if(pSceneTouchEvent.isActionUp()){
			if(State_Menu_Current==STATE_MENU_MAIN){
				if(pTouchArea.equals(graphic.spr_Button_Menu_Pressed[BUTTON_PLAY])){
					if(playDown==true){
						graphic.spr_Button_Menu_Pressed[BUTTON_PLAY].setVisible(false);
						detachMenu();
						switchState(STATE_MENU_STAGE);
					}else{
						graphic.spr_Button_Menu[BUTTON_CREDIT].setVisible(true);
						graphic.spr_Button_Menu_Pressed[BUTTON_CREDIT].setVisible(false);
						graphic.spr_Button_Menu[BUTTON_HELP].setVisible(true);
						graphic.spr_Button_Menu_Pressed[BUTTON_HELP].setVisible(false);
//						graphic.spr_Button_Menu[BUTTON_CREDIT].setVisible(true);
//						graphic.spr_Button_Menu_Pressed[BUTTON_CREDIT].setVisible(false);
					}
					
				}if(!pTouchArea.equals(graphic.spr_Button_Menu_Pressed[BUTTON_PLAY])){
					graphic.spr_Button_Menu[BUTTON_PLAY].setVisible(true);
					graphic.spr_Button_Menu_Pressed[BUTTON_PLAY].setVisible(false);
				}if(pTouchArea.equals(graphic.spr_Button_Menu_Pressed[BUTTON_CREDIT])){
					if(CreditDown==true){
						graphic.spr_Button_Menu_Pressed[BUTTON_CREDIT].setVisible(false);
						detachMenu();
						switchState(STATE_MENU_CREDIT);
					}else{
						graphic.spr_Button_Menu[BUTTON_PLAY].setVisible(true);
						graphic.spr_Button_Menu_Pressed[BUTTON_PLAY].setVisible(false);
						graphic.spr_Button_Menu[BUTTON_HELP].setVisible(true);
						graphic.spr_Button_Menu_Pressed[BUTTON_HELP].setVisible(false);
					}
				}
				if(!pTouchArea.equals(graphic.spr_Button_Menu_Pressed[BUTTON_CREDIT])){
					graphic.spr_Button_Menu[BUTTON_CREDIT].setVisible(true);
					graphic.spr_Button_Menu_Pressed[BUTTON_CREDIT].setVisible(false);
					}
				if(pTouchArea.equals(graphic.spr_Button_Menu_Pressed[BUTTON_HELP])){
					if(HelpDown==true){
						graphic.spr_Button_Menu_Pressed[BUTTON_HELP].setVisible(false);
						detachMenu();
						switchState(STATE_MENU_HELP);
					}else{
						graphic.spr_Button_Menu[BUTTON_CREDIT].setVisible(true);
						graphic.spr_Button_Menu_Pressed[BUTTON_CREDIT].setVisible(false);
						graphic.spr_Button_Menu[BUTTON_PLAY].setVisible(true);
						graphic.spr_Button_Menu_Pressed[BUTTON_PLAY].setVisible(false);
					}
				}if(!pTouchArea.equals(graphic.spr_Button_Menu_Pressed[BUTTON_HELP])){
					graphic.spr_Button_Menu[BUTTON_HELP].setVisible(true);
					graphic.spr_Button_Menu_Pressed[BUTTON_HELP].setVisible(false);
					}
//			if(pTouchArea.equals(graphic.spr_Button_Menu_Pressed[BUTTON_HELP])){
//				String url = "https://play.google.com/store/search?q=amikomgamedev&c=apps";
//				Intent start = new Intent(Intent.ACTION_VIEW);
//				start.setData(Uri.parse(url));
////				mainActivit.startActivity(start);
//			}
			}
		}
		}
		return super.onAreaTouched(pSceneTouchEvent, pTouchArea, pTouchAreaLocalX, pTouchAreaLocalY);
	}

	private void detachMenu()
	{
		mEngine.runOnUpdateThread(new Runnable()
		{

			@Override
			public void run()
			{
				// TODO Auto-generated method stub
			graphic.detachMenuUtama();
			text.detachBetaVersion();
			}
		});
		
	}
}
